﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Accounting.Library;
using Converter;
using System.Resources;
using System.Reflection;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_BoPhan;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_LoaiTSCD;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_PhuongPhapTKH;

namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_KeKhai
{
    public partial class TSCD_KeKhai_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "TSCD_KeKhai"; }
        }

        #endregion

        #region Switch View Methods
        private void SwitchInputForInformationView(string nameOfView)
        {
            if (nameOfView == "GeneralInfoView")
            {
                this.TSCD_KeKhai_GeneralInformation_GroupBox.Visibility = Visibility.Visible;
                this.TSCD_KeKhai_DetailInformation_GroupBox.Visibility = Visibility.Collapsed;
            }
            else if (nameOfView == "DetailInfoView")
            {
                this.TSCD_KeKhai_DetailInformation_GroupBox.Visibility = Visibility.Visible;
                this.TSCD_KeKhai_GeneralInformation_GroupBox.Visibility = Visibility.Collapsed;
            }
        }

        private void DetailInfoView_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            SwitchInputForInformationView("DetailInfoView");
        }

        private void GeneralInfoView_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            SwitchInputForInformationView("GeneralInfoView");
        }
        #endregion

        #region Load Methods
        public TSCD_KeKhai_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_KeKhai.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.TSCD_KeKhai_GeneralInformation_GroupBox, controls);

            this.TSCD_KeKhai_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource TSCD_KeKhai_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
                TSCD_KeKhai_ListViewModel TSCD_KeKhai_ListViewModel = new TSCD_KeKhai_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                TSCD_KeKhai_collectionViewSource.Source = new List<object> { TSCD_KeKhai_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_LoaiTSCD_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTSCD_ListViewModelSource"];
                DM_LoaiTSCD_ListViewModel dm_LoaiTSCD_ListViewModel = new DM_LoaiTSCD_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_LoaiTSCD_collectionViewSource.Source = new List<object> { dm_LoaiTSCD_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_NhaCungCap_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                DM_NhaCungCap_collectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_BoPhan_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_BoPhan_ListViewModelSource"];
                DM_BoPhan_ListViewModel dm_BoPhan_ListViewModel = new DM_BoPhan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_BoPhan_collectionViewSource.Source = new List<object> { dm_BoPhan_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_TaiKhoan_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_TaiKhoan_collectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_PhuongPhapTKH_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongPhapTKH_ListViewModelSource"];
                DM_PhuongPhapTKH_ListViewModel dm_PhuongPhapTKH_ListViewModel = new DM_PhuongPhapTKH_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_PhuongPhapTKH_collectionViewSource.Source = new List<object> { dm_PhuongPhapTKH_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_DoiTuongTHCP_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_DoiTuongTHCP_collectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource TSCD_KeKhai_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ViewModelSource"];
                TSCD_KeKhai_ViewModel TSCD_KeKhai_ViewModel = new TSCD_KeKhai_ViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                TSCD_KeKhai_ViewSource.Source = new List<object> { TSCD_KeKhai_ViewModel };
                TSCD_KeKhai_ViewModel.Saved += new EventHandler(TSCD_KeKhai_ViewModel_Saved);
                
            }
        }
        #endregion

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
        }

       

        private void TSCD_KeKhai_GeneralInformation_LoaiTSCDId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTSCD_ChildWindow dm_LoaiTSCD_ChildWindow = new DM_LoaiTSCD_ChildWindow();
            dm_LoaiTSCD_ChildWindow.Show();
            dm_LoaiTSCD_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTSCD_ViewModel_Saved);

        }

        private void TSCD_KeKhai_GeneralInformation_BoPhanId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            
            DM_BoPhan_ChildWindow dm_BoPhan_ChildWindow = new DM_BoPhan_ChildWindow();
            dm_BoPhan_ChildWindow.Show();
            dm_BoPhan_ChildWindow.SaveClicked += new EventHandler(dm_BoPhan_ViewModel_Saved);
        }

        private void TSCD_KeKhai_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);

        }

        private void TSCD_KeKhai_TaiKhoanInformation_TkKhauHaoId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TaiKhoan_ChildWindow dm_TaiKhoan_ChildWindow = new DM_TaiKhoan_ChildWindow();
            dm_TaiKhoan_ChildWindow.Show();
            dm_TaiKhoan_ChildWindow.SaveClicked += new EventHandler(dm_TaiKhoan_ViewModel_Saved);

        }

        private void TSCD_KeKhai_TaiKhoanInformation_TkNguyenGiaId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TaiKhoan_ChildWindow dm_TaiKhoan_ChildWindow = new DM_TaiKhoan_ChildWindow();
            dm_TaiKhoan_ChildWindow.Show();
            dm_TaiKhoan_ChildWindow.SaveClicked += new EventHandler(dm_TaiKhoan_ViewModel_Saved);
        }

        private void TSCD_KeKhai_TaiKhoanInformation_TkChiPhiId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_TaiKhoan_ChildWindow dm_TaiKhoan_ChildWindow = new DM_TaiKhoan_ChildWindow();
            dm_TaiKhoan_ChildWindow.Show();
            dm_TaiKhoan_ChildWindow.SaveClicked += new EventHandler(dm_TaiKhoan_ViewModel_Saved);
        }

        private void TSCD_KeKhai_NguyenGiaVaKhauHaoInformation_PhuongPhapTKHId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_PhuongPhapTKH_ChildWindow dm_PhuongPhapTKH_ChildWindow = new DM_PhuongPhapTKH_ChildWindow();
            dm_PhuongPhapTKH_ChildWindow.Show();
            dm_PhuongPhapTKH_ChildWindow.SaveClicked += new EventHandler(dm_PhuongPhapTKH_ViewModel_Saved);
        }
        #region View Model Related Methods

        void TSCD_KeKhai_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource TSCD_KeKhai_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ListViewModelSource"];
            TSCD_KeKhai_ListViewModel vm = ((List<object>)TSCD_KeKhai_ListViewSource.Source)[0] as TSCD_KeKhai_ListViewModel;
            vm.GetTSCD_KeKhai_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
        }

        void dm_BoPhan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_BoPhan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_BoPhan_ListViewModelSource"];
            DM_BoPhan_ListViewModel vm = ((List<object>)dm_BoPhan_ListViewSource.Source)[0] as DM_BoPhan_ListViewModel;
            vm.GetDM_BoPhan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_PhuongPhapTKH_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_PhuongPhapTKH_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongPhapTKH_ListViewModelSource"];
            DM_PhuongPhapTKH_ListViewModel vm = ((List<object>)dm_PhuongPhapTKH_ListViewSource.Source)[0] as DM_PhuongPhapTKH_ListViewModel;
            vm.GetDM_PhuongPhapTKH_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_LoaiTSCD_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTSCD_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTSCD_ListViewModelSource"];
            DM_LoaiTSCD_ListViewModel vm = ((List<object>)dm_LoaiTSCD_ListViewSource.Source)[0] as DM_LoaiTSCD_ListViewModel;
            vm.GetDM_LoaiTSCD_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_TaiKhoan_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_TaiKhoan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel vm = ((List<object>)dm_TaiKhoan_ListViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            vm.GetDM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        private void OnCellDoubleClick(object sender, RadRoutedEventArgs args)
        {
            if (this.TSCD_KeKhai_GridView.SelectedItem != null)
            {
                System.Windows.Data.CollectionViewSource TSCD_KeKhai_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TSCD_KeKhai_ViewModelSource"];
                TSCD_KeKhai_ViewModel vm = ((List<object>)TSCD_KeKhai_ViewSource.Source)[0] as TSCD_KeKhai_ViewModel;
                int parameter = ((TSCD_KeKhaiInfo)this.TSCD_KeKhai_GridView.SelectedItem).Khoa;
                Csla.Xaml.ExecuteEventArgs eventObject = new Csla.Xaml.ExecuteEventArgs();
                eventObject.MethodParameter = parameter;
                vm.GetTSCD_KeKhai(this, eventObject);
            }
        }
        #endregion
    }
}
